/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TimeRecording.tables;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.PostLoad;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author cbegg
 */
@Entity
@Table(name = "working_summary")
@NamedQueries({
    @NamedQuery(
            name = "WorkingSummary.findAll",
            query = "SELECT w " +
                    "FROM WorkingSummary w"),
    @NamedQuery(
            name = "WorkingSummary.findByEmployeeId",
            query = "SELECT w " +
                    "FROM WorkingSummary w " +
                    "WHERE w.employeeId = :employeeId"),
    @NamedQuery(
            name = "WorkingSummary.findByAlertSuppression",
            query = "SELECT w " +
                    "FROM WorkingSummary w " +
                    "WHERE w.alertSuppression = :alertSuppression"),
    @NamedQuery(
            name = "WorkingSummary.findByNumberOfPeriods",
            query = "SELECT w " +
                    "FROM WorkingSummary w " +
                    "WHERE w.numberOfPeriods = :numberOfPeriods")})
public class WorkingSummary implements Serializable {
    
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    @Column(name = "employee_id")
    private Integer employeeId;

    @Lob
    @Column(name = "pattern_description")
    private String patternDescription;

    @Column(name = "alert_suppression")
    private Boolean alertSuppression;

    @Basic(optional = false)
    @Column(name = "number_of_periods")
    private short numberOfPeriods;

    @Column(name = "hours_worked_per_week")
    private double hoursWorkedPerWeek;

    @Column(name = "hours_worked_per_day")
    private double hoursWorkedPerDay;

    @Column(name = "av_last_year_hours")
    private double lastAnnualLeaveAllowanceHours;

    @Column(name = "av_last_year_days")
    private double lastAnnualLeaveAllowanceDays;

    @Column(name = "av_this_year_hours")
    private double annualLeaveAllowanceHours;

    @Column(name = "av_this_year_days")
    private double annualLeaveAllowanceDays;

    @Column(name = "av_next_year_hours")
    private double nextAnnualLeaveAllowanceHours;

    @Column(name = "av_next_year_days")
    private double nextAnnualLeaveAllowanceDays;

    @Column(name = "av_brought_forward")
    private double annualLeaveBroughtForward;

    @Column(name = "av_granted")
    private double annualLeaveGranted;

    @Column(name = "av_approved")
    private double annualLeaveApproved;

    @Column(name = "av_unapproved")
    private double annualLeaveUnapproved;

    @Column(name = "av_taken")
    private double annualLeaveTaken;

    @Column(name = "av_remaining")
    private double annualLeaveRemaining;

    @JoinColumn(name = "employee_id", referencedColumnName = "employee_id", insertable = false, updatable = false)
    @OneToOne(optional = false)
    private Employee employees;

    @Transient
    private double annualLeaveBroughtForwardInDays;

    @Transient
    private double annualLeaveGrantedInDays;

    @Transient
    private double annualLeaveApprovedInDays;

    @Transient
    private double annualLeaveUnapprovedInDays;

    @Transient
    private double annualLeaveTakenInDays;

    @Transient
    private double annualLeaveRemainingInDays;

    public WorkingSummary() {
        alertSuppression = false;
        patternDescription = "Not yet defined";
    }

    public Integer getEmployeeId() {
	return employeeId;
    }

    public void setEmployeeId(Integer employeeId) {
	Integer oldemployeeId = this.employeeId;
	this.employeeId = employeeId;
	changeSupport.firePropertyChange("employeeId", oldemployeeId, employeeId);
    }

    public String getPatternDescription() {
	return patternDescription;
    }

    public void setPatternDescription(String patternDescription) {
	String oldpatternDescription = this.patternDescription;
	this.patternDescription = patternDescription;
	changeSupport.firePropertyChange("patternDescription", oldpatternDescription, patternDescription);
    }

    public Boolean getAlertSuppression() {
	return alertSuppression;
    }

    public void setAlertSuppression(Boolean alertSuppression) {
	Boolean oldalertSuppression = this.alertSuppression;
	this.alertSuppression = alertSuppression;
	changeSupport.firePropertyChange("alertSuppression", oldalertSuppression, alertSuppression);
    }

    public short getNumberOfPeriods() {
	return numberOfPeriods;
    }

    public void setNumberOfPeriods(short numberOfPeriods) {
	short oldnumberOfPeriods = this.numberOfPeriods;
	this.numberOfPeriods = numberOfPeriods;
	changeSupport.firePropertyChange("numberOfPeriods", oldnumberOfPeriods, numberOfPeriods);
    }

    public double getHoursWorkedPerWeek() {
	return hoursWorkedPerWeek;
    }

    public void setHoursWorkedPerWeek(double hoursWorkedPerWeek) {
	double oldHoursWorkedPerWeek = this.hoursWorkedPerWeek;
	this.hoursWorkedPerWeek = hoursWorkedPerWeek;
	changeSupport.firePropertyChange("hoursWorkedPerWeek", oldHoursWorkedPerWeek, hoursWorkedPerWeek);
    }

    public double getHoursWorkedPerDay() {
	return hoursWorkedPerDay;
    }

    public void setHoursWorkedPerDay(double hoursWorkedPerDay) {
	double oldHoursWorkedPerDay = this.hoursWorkedPerDay;
	this.hoursWorkedPerDay = hoursWorkedPerDay;
	changeSupport.firePropertyChange("hoursWorkedPerDay", oldHoursWorkedPerDay, hoursWorkedPerDay);

        // Refresh all of the dependant "in days" class members
        setAnnualLeaveBroughtForwardInDays();
        setAnnualLeaveGrantedInDays();
        setAnnualLeaveApprovedInDays();
        setAnnualLeaveUnapprovedInDays();
        setAnnualLeaveTakenInDays();
        setAnnualLeaveRemainingInDays();
    }

    // Last year's annual leave allowance

    public double getLastAnnualLeaveAllowanceHours() {
	return lastAnnualLeaveAllowanceHours;
    }

    public void setLastAnnualLeaveAllowanceHours(double lastAnnualLeaveAllowanceHours) {
	double oldLastAnnualLeaveAllowanceHours = this.lastAnnualLeaveAllowanceHours;
	this.lastAnnualLeaveAllowanceHours = lastAnnualLeaveAllowanceHours;
	changeSupport.firePropertyChange("lastAnnualLeaveAllowanceHours",
                oldLastAnnualLeaveAllowanceHours, lastAnnualLeaveAllowanceHours);
    }

    public double getLastAnnualLeaveAllowanceDays() {
	return lastAnnualLeaveAllowanceDays;
    }

    public void setLastAnnualLeaveAllowanceDays(double lastAnnualLeaveAllowanceDays) {
	double oldLastAnnualLeaveAllowanceDays = this.lastAnnualLeaveAllowanceDays;
	this.lastAnnualLeaveAllowanceDays = lastAnnualLeaveAllowanceDays;
	changeSupport.firePropertyChange("lastAnnualLeaveAllowanceDays",
                oldLastAnnualLeaveAllowanceDays, lastAnnualLeaveAllowanceDays);
    }

    // This year's annual leave allowance
    
    public double getAnnualLeaveAllowanceHours() {
	return annualLeaveAllowanceHours;
    }

    public void setAnnualLeaveAllowanceHours(double annualLeaveAllowanceHours) {
	double oldAnnualLeaveAllowanceHours = this.annualLeaveAllowanceHours;
	this.annualLeaveAllowanceHours = annualLeaveAllowanceHours;
	changeSupport.firePropertyChange("annualLeaveAllowanceHours",
                oldAnnualLeaveAllowanceHours, annualLeaveAllowanceHours);
    }

    public double getAnnualLeaveAllowanceDays() {
	return annualLeaveAllowanceDays;
    }

    public void setAnnualLeaveAllowanceDays(double annualLeaveAllowanceDays) {
	double oldAnnualLeaveAllowanceDays = this.annualLeaveAllowanceDays;
	this.annualLeaveAllowanceDays = annualLeaveAllowanceDays;
	changeSupport.firePropertyChange("annualLeaveAllowanceDays",
                oldAnnualLeaveAllowanceDays, annualLeaveAllowanceDays);
    }

    // Next year's annual leave allowance

    public double getNextAnnualLeaveAllowanceHours() {
	return nextAnnualLeaveAllowanceHours;
    }

    public void setNextAnnualLeaveAllowanceHours(double nextAnnualLeaveAllowanceHours) {
	double oldNextAnnualLeaveAllowanceHours = this.nextAnnualLeaveAllowanceHours;
	this.nextAnnualLeaveAllowanceHours = nextAnnualLeaveAllowanceHours;
	changeSupport.firePropertyChange("nextAnnualLeaveAllowanceHours",
                oldNextAnnualLeaveAllowanceHours, nextAnnualLeaveAllowanceHours);
    }

    public double getNextAnnualLeaveAllowanceDays() {
	return nextAnnualLeaveAllowanceDays;
    }

    public void setNextAnnualLeaveAllowanceDays(double nextAnnualLeaveAllowanceDays) {
	double oldNextAnnualLeaveAllowanceDays = this.nextAnnualLeaveAllowanceDays;
	this.nextAnnualLeaveAllowanceDays = nextAnnualLeaveAllowanceDays;
	changeSupport.firePropertyChange("nextAnnualLeaveAllowanceDays",
                oldNextAnnualLeaveAllowanceDays, nextAnnualLeaveAllowanceDays);
    }

    // Working figures for annual leave

    public double getAnnualLeaveBroughtForward() {
	return annualLeaveBroughtForward;
    }

    public void setAnnualLeaveBroughtForward(double annualLeaveBroughtForward) {
	double oldAnnualLeaveBroughtForward = this.annualLeaveBroughtForward;
	this.annualLeaveBroughtForward = annualLeaveBroughtForward;
	changeSupport.firePropertyChange("annualLeaveBroughtForward",
                oldAnnualLeaveBroughtForward, annualLeaveBroughtForward);
        setAnnualLeaveBroughtForwardInDays();
    }

    public double getAnnualLeaveBroughtForwardInDays() {
	return annualLeaveBroughtForwardInDays;
    }

    public void setAnnualLeaveBroughtForwardInDays() {
        if (getHoursWorkedPerDay() != 0) {
            setAnnualLeaveBroughtForwardInDays(
                    getAnnualLeaveBroughtForward() / getHoursWorkedPerDay());
        } else {
            setAnnualLeaveBroughtForwardInDays(0);
        }
    }

    public void setAnnualLeaveBroughtForwardInDays(double annualLeaveBroughtForwardInDays) {
	double oldAnnualLeaveBroughtForwardInDays = this.annualLeaveBroughtForwardInDays;
	this.annualLeaveBroughtForwardInDays = annualLeaveBroughtForwardInDays;
	changeSupport.firePropertyChange("annualLeaveBroughtForwardInDays",
                oldAnnualLeaveBroughtForwardInDays, annualLeaveBroughtForwardInDays);
    }

    public double getAnnualLeaveGranted() {
	return annualLeaveGranted;
    }

    public void setAnnualLeaveGranted(double annualLeaveGranted) {
	double oldAnnualLeaveGranted = this.annualLeaveGranted;
	this.annualLeaveGranted = annualLeaveGranted;
	changeSupport.firePropertyChange("annualLeaveGranted",
                oldAnnualLeaveGranted, annualLeaveGranted);
        setAnnualLeaveGrantedInDays();
    }

    public double getAnnualLeaveGrantedInDays() {
	return annualLeaveGrantedInDays;
    }

    public void setAnnualLeaveGrantedInDays() {
        if (getHoursWorkedPerDay() != 0) {
            setAnnualLeaveGrantedInDays(
                    getAnnualLeaveGranted() / getHoursWorkedPerDay());
        } else {
            setAnnualLeaveGrantedInDays(0);
        }
    }

    public void setAnnualLeaveGrantedInDays(double annualLeaveGrantedInDays) {
	double oldAnnualLeaveGrantedInDays = this.annualLeaveGrantedInDays;
	this.annualLeaveGrantedInDays = annualLeaveGrantedInDays;
	changeSupport.firePropertyChange("annualLeaveGrantedInDays",
                oldAnnualLeaveGrantedInDays, annualLeaveGrantedInDays);
    }

    public double getAnnualLeaveApproved() {
	return annualLeaveApproved;
    }

    public void setAnnualLeaveApproved(double annualLeaveApproved) {
	double oldAnnualLeaveApproved = this.annualLeaveApproved;
	this.annualLeaveApproved = annualLeaveApproved;
	changeSupport.firePropertyChange("annualLeaveApproved",
                oldAnnualLeaveApproved, annualLeaveApproved);
        setAnnualLeaveApprovedInDays();
    }

    public double getAnnualLeaveApprovedInDays() {
	return annualLeaveApprovedInDays;
    }

    public void setAnnualLeaveApprovedInDays() {
        if (getHoursWorkedPerDay() != 0) {
            setAnnualLeaveApprovedInDays(
                    getAnnualLeaveApproved() / getHoursWorkedPerDay());
        } else {
            setAnnualLeaveApprovedInDays(0);
        }
    }

    public void setAnnualLeaveApprovedInDays(double annualLeaveApprovedInDays) {
	double oldAnnualLeaveApprovedInDays = this.annualLeaveApprovedInDays;
	this.annualLeaveApprovedInDays = annualLeaveApprovedInDays;
	changeSupport.firePropertyChange("annualLeaveApprovedInDays",
                oldAnnualLeaveApprovedInDays, annualLeaveApprovedInDays);
    }

    public double getAnnualLeaveUnapproved() {
	return annualLeaveUnapproved;
    }

    public void setAnnualLeaveUnapproved(double annualLeaveUnapproved) {
	double oldAnnualLeaveUnapproved = this.annualLeaveUnapproved;
	this.annualLeaveUnapproved = annualLeaveUnapproved;
	changeSupport.firePropertyChange("annualLeaveUnapproved",
                oldAnnualLeaveUnapproved, annualLeaveUnapproved);
        setAnnualLeaveUnapprovedInDays();
    }

    public double getAnnualLeaveUnapprovedInDays() {
	return annualLeaveUnapprovedInDays;
    }

    public void setAnnualLeaveUnapprovedInDays() {
        if (getHoursWorkedPerDay() != 0) {
            setAnnualLeaveUnapprovedInDays(
                    getAnnualLeaveUnapproved() / getHoursWorkedPerDay());
        } else {
            setAnnualLeaveUnapprovedInDays(0);
        }
    }

    public void setAnnualLeaveUnapprovedInDays(double annualLeaveUnapprovedInDays) {
	double oldAnnualLeaveUnapprovedInDays = this.annualLeaveUnapprovedInDays;
	this.annualLeaveUnapprovedInDays = annualLeaveUnapprovedInDays;
	changeSupport.firePropertyChange("annualLeaveUnapprovedInDays",
                oldAnnualLeaveUnapprovedInDays, annualLeaveUnapprovedInDays);
    }

    public double getAnnualLeaveTaken() {
	return annualLeaveTaken;
    }

    public void setAnnualLeaveTaken(double annualLeaveTaken) {
	double oldAnnualLeaveTaken = this.annualLeaveTaken;
	this.annualLeaveTaken = annualLeaveTaken;
	changeSupport.firePropertyChange("annualLeaveTaken",
                oldAnnualLeaveTaken, annualLeaveTaken);
        setAnnualLeaveTakenInDays();
    }

    public double getAnnualLeaveTakenInDays() {
	return annualLeaveTakenInDays;
    }

    public void setAnnualLeaveTakenInDays() {
        if (getHoursWorkedPerDay() != 0) {
            setAnnualLeaveTakenInDays(
                    getAnnualLeaveTaken() / getHoursWorkedPerDay());
        } else {
            setAnnualLeaveTakenInDays(0);
        }
    }

    public void setAnnualLeaveTakenInDays(double annualLeaveTakenInDays) {
	double oldAnnualLeaveTakenInDays = this.annualLeaveTakenInDays;
	this.annualLeaveTakenInDays = annualLeaveTakenInDays;
	changeSupport.firePropertyChange("annualLeaveTakenInDays",
                oldAnnualLeaveTakenInDays, annualLeaveTakenInDays);
    }

    public double getAnnualLeaveRemaining() {
	return annualLeaveRemaining;
    }

    public void setAnnualLeaveRemaining(double annualLeaveRemaining) {
	double oldAnnualLeaveRemaining = this.annualLeaveRemaining;
	this.annualLeaveRemaining = annualLeaveRemaining;
	changeSupport.firePropertyChange("annualLeaveRemaining",
                oldAnnualLeaveRemaining, annualLeaveRemaining);
        setAnnualLeaveRemainingInDays();
    }

    public double getAnnualLeaveRemainingInDays() {
	return annualLeaveRemainingInDays;
    }

    public void setAnnualLeaveRemainingInDays() {
        if (getHoursWorkedPerDay() != 0) {
            setAnnualLeaveRemainingInDays(
                    getAnnualLeaveRemaining() / getHoursWorkedPerDay());
        } else {
            setAnnualLeaveRemainingInDays(0);
        }
    }

    public void setAnnualLeaveRemainingInDays(double annualLeaveRemainingInDays) {
	double oldAnnualLeaveRemainingInDays = this.annualLeaveRemainingInDays;
	this.annualLeaveRemainingInDays = annualLeaveRemainingInDays;
	changeSupport.firePropertyChange("annualLeaveRemainingInDays",
                oldAnnualLeaveRemainingInDays, annualLeaveRemainingInDays);
    }

    public Employee getEmployees() {
	return employees;
    }

    public void setEmployees(Employee employees) {
	Employee oldemployees = this.employees;
	this.employees = employees;
	changeSupport.firePropertyChange("employees", oldemployees, employees);
    }

    @PostLoad
    public void refreshTransientMembers() {
        setAnnualLeaveBroughtForwardInDays();
        setAnnualLeaveGrantedInDays();
        setAnnualLeaveApprovedInDays();
        setAnnualLeaveUnapprovedInDays();
        setAnnualLeaveTakenInDays();
        setAnnualLeaveRemainingInDays();
    }

    @Override
    public int hashCode() {
	int hash = 0;
	hash += (employeeId != null ? employeeId.hashCode() : 0);
	return hash;
    }

    @Override
    public boolean equals(Object object) {
	if (!(object instanceof WorkingSummary))	{
	    return false;
	}
	WorkingSummary other = (WorkingSummary) object;
	if ((this.employeeId == null && other.employeeId != null) ||
                (this.employeeId != null && !this.employeeId.equals(other.employeeId))) {
	    return false;
	}
	return true;
    }

    @Override
    public String toString() {
	return "TimeRecording.tables.WorkingSummary[employeeId=" + employeeId + "]";
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
	changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
	changeSupport.removePropertyChangeListener(listener);
    }
}